package com.briup.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.demo.bean.XcdGoods;
import com.briup.demo.mapper.XcdGoodsMapper;

@Service
public class XcdGoodsService {
	
	@Autowired
	private XcdGoodsMapper goodsMapper;
	
	public List<XcdGoods> getAllGoods(){
		List<XcdGoods> list = goodsMapper.selectAll();
		if(list!=null) {
			return list;
		}
		return new ArrayList<XcdGoods>();
	}
	
	public int saveOrUpdate(XcdGoods goods) {
		XcdGoods xg = goodsMapper.selectByPrimaryKey(goods.getGoodsid());
		if(goods.getGoodsid()==null) {
			goods.setGoodsid(UUID.randomUUID().toString()) ;
			goodsMapper.insert(goods);
			return 1;
		}else if(xg == null) {
			goodsMapper.insert(goods);
			return 1;
		}else {
			goodsMapper.updateByPrimaryKey(goods);
			return 0;
		}
	}
	
	public int deleteById(String id) {
		return goodsMapper.deleteByPrimaryKey(id);
	}
	
	public XcdGoods selectById(String id) {
		return goodsMapper.selectByPrimaryKey(id);
	}
	
//	public List<XcdGoods> selectByName(String Name) {
//		List<Xcd>	}
//	 
	public int deleteByIds(String[] ids) {
		int count=0;
		for(String id:ids) {
			if(deleteById(id)==1) {
				count++;
			}
		}
		return count;
	}
	
	public XcdGoods selectByName(String name) {
		return goodsMapper.selectByName(name);
	}

}
